<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #box {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        #box1 {
            position: absolute;
            top: 150px;
            width: 200px;
            height: 200px;
            background-color: purple;
        }
    </style>
</head>
<body>
<button id="btn200">200</button>
<button id="btn400">400</button>
<div id="box"></div>
<div id="box1"></div>
</body>
</html>
<script>
    var box = document.getElementById("box");
    var box1 = document.getElementById("box1");
    var btn200 = document.getElementById("btn200");
    var btn400 = document.getElementById("btn400");
    btn200.onclick = function() {
        animate(box,200);
        animate(box1,500);
    }
    btn400.onclick = function() {
        animate(box,400);
    }
    //  封装匀速运动
    function animate(obj,target){
        clearInterval(obj.timer);  // 先清除定时器
        var speed = obj.offsetLeft < target ? 5 : -5;  // 用来判断 应该 +  还是 -
        obj.timer = setInterval(function() {
            var result = target - obj.offsetLeft; // 因为他们的差值不会超过5
            obj.style.left = obj.offsetLeft + speed + "px";
            if(Math.abs(result)<=5)  // 如果差值不小于 5 说明到位置了
            {
                clearInterval(obj.timer);
                obj.style.left = target + "px";  // 有5像素差距   我们直接跳转目标位置
            }
        },30)
    }
</script>